package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.orderVo.MemberOrdertvo;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author lzc
 * @since 2021-01-12
 */
@RestController
@RequestMapping("/educenter/member")
public class UcenterMemberController {


    @Autowired
    private UcenterMemberService ucenterMemberService;


    /**
     * 前端登录返回token
     */
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember){
       String token=ucenterMemberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    /**
     * 前端注册
     */
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    /**
     * 或获取用户信息
     * @return
     */
    @GetMapping("memberInfo")
    public R memberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        if(!ObjectUtils.isEmpty(ucenterMember)){
            return R.ok().data("memberInfo",ucenterMember);
        }else {
            return R.error();
        }

    }
    /**
     * 或获取用户信息根据id
     * @return
     */
    @GetMapping("/memberOrderInfo/{memberId}")
    public MemberOrdertvo memberInfo(@PathVariable("memberId") String memberId){
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        MemberOrdertvo memberOrdertvo = new MemberOrdertvo();
        BeanUtils.copyProperties(ucenterMember,memberOrdertvo);
        return memberOrdertvo;
    }

    /**
     * 查询注册统计人数
     * @return
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable("day") String day){
       Integer count=ucenterMemberService.countRegister(day);
        return R.ok().data("countRegister",count);
    }

}

